/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.display;

import java.util.Arrays;
import java.util.HashMap;

import com.googlecode.parallelrl.Config;
import com.googlecode.parallelrl.data.ecs.ECSManager;
import com.googlecode.parallelrl.process.BehaviourManager;
import com.googlecode.parallelrl.process.Game;
import com.googlecode.parallelrl.util.ComponentPair;
import com.googlecode.parallelrl.util.Point2D;

/**
 * @author Roger Cockshutt
 *
 */
public class StatusReadout {
	
	private static StatusReadout instance = null;
	
	private StatusReadout() {
		size = Config.statusWindowSize;
		position = Config.statusWindowPosition;;
		fields.put("FormalName", "Name: ");
		fields.put("Health", "Health: ");
		fields.put("Step", "Time: ");
		fields.put("iFPS", "iFPS: ");
		fields.put("aFPS", "aFPS: ");
		fields.put("Frame", "Frame: ");
		fields.put("FrameDuration", "iFrameLength: ");
		fields.put("aFrameLength", "aFrameLength: ");
	}
	
	public static StatusReadout get() {
		if (instance == null) {
			instance = new StatusReadout();
		}
		return instance;
	}
	
	private final Point2D size;
	private final Point2D position;
	private final HashMap<String, String> fields = new HashMap<>();
	private final String[] order = {
			"Frame",
			"aFPS",
			"iFPS",
			"FrameDuration",
			"aFrameLength",
			"Step",
			"FormalName",
			"Health",
			"Position",
			"Agility",
			"Strength",
			"Speed"
	};
	
	
	public void update() {
		//int ySize = size_.getY();
		int xSize = size.x;
		int yPos = position.y;
		int xPos = position.x;
		
		
		char[] clr = new char[xSize];
		Arrays.fill(clr, ' ');
		String clearLine = new String(clr);
		for (ComponentPair cmp : ECSManager.getAllComponents(BehaviourManager.getLastActiveControllable())) {
			if (fields.containsKey(cmp.name())) {
				int y = Arrays.asList(order).indexOf(cmp.name());
				Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
				Screen.get().puts(fields.get(cmp.name()) + cmp.value(), y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
			}
		}
		int y = Arrays.asList(order).indexOf("Step");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("Step") + Game.currentStep, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		y = Arrays.asList(order).indexOf("iFPS");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("iFPS") + Game.currentFPS, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		y = Arrays.asList(order).indexOf("aFPS");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("aFPS") + Game.averageFPS, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		y = Arrays.asList(order).indexOf("Frame");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("Frame") + Game.numCycles, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		y = Arrays.asList(order).indexOf("FrameDuration");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("FrameDuration") + Game.lastCycleDuration, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		y = Arrays.asList(order).indexOf("aFrameLength");
		Screen.get().puts(clearLine, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
		Screen.get().puts(fields.get("aFrameLength") + Game.averageCycleDuration, y + yPos, 0 + xPos, Colors.libTCOD.white.normal, Colors.libTCOD.black.normal);
	}

}
